package com.wjh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjh.enums.AppHttpCodeEnum;
import com.wjh.exception.SystemException;
import com.wjh.mapper.FloristMapper;
import com.wjh.pojo.Florist;
import com.wjh.pojo.Flower;
import com.wjh.service.FloristService;
import com.wjh.service.FlowerTypeService;
import com.wjh.utils.ResponseResult;
import com.wjh.vo.PageVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 花店名称(Florist)表服务实现类
 *
 * @author makejava
 * @since 2023-10-16 19:16:50
 */
@Service("floristService")
public class FloristServiceImpl extends ServiceImpl<FloristMapper, Florist> implements FloristService {

    @Resource
    private FlowerTypeService flowerTypeService;

    @Override
    public ResponseResult queryList(Integer pageNum, Integer pageSize, String name) {
        LambdaQueryWrapper<Florist> queryWrapper = new LambdaQueryWrapper<Florist>();
        queryWrapper.like(name != null,Florist::getName,name);
        //分页查询所有花店
        Page<Florist> certificatePage = new Page<>(pageNum,pageSize);
        Page<Florist> page = page(certificatePage, queryWrapper);
        return ResponseResult.okResult(new PageVo(page.getRecords(),page.getTotal()));
    }

    @Override
    public Florist queryById(Integer id) {
        Florist florist = getById(id);
        return florist;
    }

    @Override
    public ResponseResult addFlorists(Florist florist) {
        if(!StringUtils.hasText(florist.getName())){
            throw new SystemException(AppHttpCodeEnum.NEED_NAME);
        }
        save(florist);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delFloristsByIds(List<Integer> id) {
        //根据花店删除品种

        flowerTypeService.delByType(id);

        LambdaQueryWrapper<Florist> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Florist::getId,id);
        remove(queryWrapper);
        return ResponseResult.okResult();
    }
}
